
#include "clusteringOnJoints.h"

ClusteringOnJoints::ClusteringOnJoints() {
   
   lista_size = 100000;
   model = 0;
   skeleton = 0;
   init();
}

ClusteringOnJoints::ClusteringOnJoints(Mesh *m) {
   
   lista_size = 100000;
   model = m;
   skeleton = 0;
   init();
}

ClusteringOnJoints::ClusteringOnJoints(Mesh *m, USkeleton *s) {
   
   lista_size = 100000;
   model = m;
   skeleton = s;
   init();
}

ClusteringOnJoints::~ClusteringOnJoints() {
   
}

void ClusteringOnJoints::init() {
   /*
   //~ lista_size=100000;
   inter_test_limit = 0.2; 
   
   infinPoint = Point3(10000,10000,10000);
   current_basis = infinPoint; // 
   Dbasis_to_i2 = -1E8;
   
   // using joints
   // curr_bas_joint = &infinPoint[0];
   curr_bas_joint = infinPoint;
   
   lista.resize(lista_size);
   */
}

void ClusteringOnJoints::execute() {
   
   assert(model !=0);
   assert(skeleton !=0);
   
   skeleton->resetAuxiliarVars();
   
   init();
   genClosestJoints();
   
}

void ClusteringOnJoints::genClosestJoints() {
   
   Vertex_iterator vi = model->vertices_begin();
   while (vi != model->vertices_end()) {
      vi->reset_clustering();
      ++vi;
   }
   
   vi = model->vertices_begin();
   while (vi != model->vertices_end()) {
      const Point3 &p = vi->point();
      float D=1e8;
      for (int i=0; i<skeleton->nJoints(); ++i) {
         const Point3 &c = skeleton->getJointPos(i);
         Vector3 vd = p-c;
         float d = sqrt(vd*vd);
         if (d<D) {
            D = d;
            vi->rigging_group = i;
         }
      }
      vi->dClosestJoint = D;
      ++vi;
   }
}
